package tree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.StringJoiner;
import java.util.stream.Collectors;

public class TreeDemo {

    // 存放字典
    private static List<String> numbers = new ArrayList<>();

    // 原始数据
    private static List<String> sourceData = new ArrayList<>();

    // 临时映射
    private static Map<String, String> mapTemp = new HashMap<>();

    public static void main(String[] args) {

        init();

        converter(sourceData);

    }

    private static void converter(List<String> source) {
        // 记录当前父级
        Stack<String> queue = new Stack<>();
        int currentIndex = 1;

        int temp = 0;
        for (int i = 0; i < source.size(); i++) {
            System.out.println(source.get(i));
            // 1 判断类型是否更改，判断上一个跟现在是为同一类型，判断下一个跟现在是否为同一类型
            if (numbers.contains(source.get(i)) && !source.get(i).contains(".")) {
                if (i > 0 && queue.lastElement().equals(source.get(temp))) {
                    currentIndex++;
                    queue.pop();
                } else {
                    currentIndex = 1;
                }
                queue.push(currentIndex + "");
                temp = i;
                mapTemp.put(String.join("-", queue), source.get(i));
            }
            // 2 判断是否含有.并且是上个的子集
        }

        System.out.println("-------");
        System.out.println(queue);
        System.out.println(mapTemp);

    }

    private static void init() {

        // 初始化数据字典
        numbers.addAll(Arrays.asList("I", "Ⅱ", "Ⅲ", "Ⅳ", "V", "", "", "", "", ""));
        numbers.addAll(Arrays.asList("一", "二", "三", "四", "五", "", "", "", "", ""));
        numbers.addAll(Arrays.asList("（一）", "（二）", "（三）", "（四）", "（五）", "", "", "", "", ""));
        numbers.addAll(Arrays.asList("1", "2", "3", "4", "5", "", "", "", "", ""));

        // 初始化原始数据
        sourceData.add("I");
        sourceData.add("一");
        sourceData.add("（一）");
        sourceData.add("1");
        sourceData.add("1.1");
        sourceData.add("1.2");
        sourceData.add("1.3");
        sourceData.add("1.3.1");
        sourceData.add("1.3.2");
        sourceData.add("1.3.2.1");
        sourceData.add("1.3.2.1.1");
        sourceData.add("1.4");
        sourceData.add("2");
        sourceData.add("2.1");
        sourceData.add("3");
        sourceData.add("3.1");
    }

}
